const axios = require('axios');

async function debugJoinActivity() {
  try {
    console.log('=== 测试活动参与API ===');
    
    // 测试多个用户和活动的组合
    const testCases = [
      { user: 'bob@student.com', activityId: 2, description: 'Bob参与活动2' },
      { user: 'charlie@student.com', activityId: 3, description: 'Charlie参与活动3' },
      { user: 'admin@campus.com', activityId: 4, description: 'Admin参与活动4' }
    ];
    
    for (const testCase of testCases) {
      console.log(`\n=== ${testCase.description} ===`);
      
      try {
        // 登录
        console.log(`1. 登录用户 ${testCase.user}...`);
        const loginResponse = await axios.post('http://localhost:3000/api/auth/login', {
          email: testCase.user,
          password: testCase.user.includes('admin') ? 'admin123' : 'password123'
        });
        
        const token = loginResponse.data.token;
        console.log('登录成功');
        
        // 参与活动
        console.log(`2. 尝试参与活动${testCase.activityId}...`);
        
        const joinResponse = await axios.post(`http://localhost:3000/api/activities/${testCase.activityId}/join`, {}, {
          headers: {
            'Authorization': `Bearer ${token}`,
            'Content-Type': 'application/json'
          }
        });
        
        console.log('✅ 参与活动成功:', joinResponse.data);
        
      } catch (error) {
        console.error('❌ 测试失败:');
        console.error('  状态码:', error.response?.status);
        console.error('  错误信息:', error.response?.data);
        
        if (error.response?.status === 500) {
          console.error('  这是500错误！需要检查服务器日志');
        }
      }
      
      // 添加延迟避免请求过快
      await new Promise(resolve => setTimeout(resolve, 500));
    }
    
  } catch (error) {
    console.error('调试过程中发生未预期错误:', error.message);
  }
}

debugJoinActivity(); 